﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRasta.Configuration;
using OpenRasta.Web;
using uREST.Core;
using uREST.Core.Services;
using uREST.Core.Resources;
using uREST.Core.Codecs;
using uREST.CMS.Resources;
using uREST.CMS.Handlers;

namespace uREST.CMS.Services
{
    public class DocumentService : IuRESTService
    {
        public EndPoint EndPoint
        {
            get
            {
                return new EndPoint
                {
                    Name = "Document Service",
                    Description = "",
                    Href = typeof(List<Document>).CreateUri("DocumentList").ToString()
                };
            }
        }

        public void RegisterHandlers()
        {
            ResourceSpace.Has.ResourcesOfType<List<Document>>()
                .AtUri(uRESTConfig.BaseUrl + "/documents").Named("DocumentList")
                .And.AtUri(uRESTConfig.BaseUrl + "/documents?doctypealias={docTypeAlias}").Named("DocumentList")
                .And.AtUri(uRESTConfig.BaseUrl + "/documents/{parentId}/documents").Named("DocumentList")
                .And.AtUri(uRESTConfig.BaseUrl + "/documents/{parentId}/documents?doctypealias={docTypeAlias}").Named("DocumentList")
                .HandledBy<DocumentHandler>()
                .TranscodedBy<JsonNetCodec>();

            ResourceSpace.Has.ResourcesOfType<Document>()
                .AtUri(uRESTConfig.BaseUrl + "/documents/{id}").Named("Document")
                .And.AtUri(uRESTConfig.BaseUrl + "/documents/{id}?permanent={permanent}").Named("Document")
                .HandledBy<DocumentHandler>()
                .TranscodedBy<JsonNetCodec>();
        }
    }
}
